package com.jshoperxms.controller.mall.backstage.nos;

import com.jshoperxms.Response;
import com.jshoperxms.components.netease.nos.NosKeys;
import com.jshoperxms.controller.mall.backstage.base.BaseTController;
import com.jshoperxms.controller.mall.backstage.base.IBaseCtrl;
import com.jshoperxms.controller.mall.backstage.base.SearchModel;
import com.jshoperxms.controller.utils.DateTools;
import com.jshoperxms.controller.utils.statickey.TagWords;
import com.netease.cloud.services.nos.NosClient;
import com.netease.cloud.services.nos.model.ListObjectsRequest;
import com.netease.cloud.services.nos.model.NOSObjectSummary;
import com.netease.cloud.services.nos.model.ObjectListing;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.util.*;

/**
 * Created by sdywcd on 2018/9/12.
 * Des:
 */
@RestController
@RequestMapping("/mall/nos/bucket/files")
public class NosBucketFilesController extends BaseTController implements IBaseCtrl<NOSObjectSummary> {

    //数据形式
    private Map<String, Object> responsedata = new HashMap<>();

    private NosClient nosClient=this.getCli();

    @Override
    public ResponseEntity save(NOSObjectSummary rq) throws ParseException {
        return null;
    }

    @Override
    public ResponseEntity find(NOSObjectSummary rq) {
        return null;
    }

    @Override
    public ResponseEntity del(NOSObjectSummary rq) {
        return null;
    }

    @Override
    public ResponseEntity update(NOSObjectSummary rq) throws ParseException {
        return null;
    }

    @Override
    public ResponseEntity findAll() {
        return null;
    }

    @RequestMapping(value="/findByPage",method= RequestMethod.POST)
    @Override
    public ResponseEntity findByPage(SearchModel searchModel, String search, String ordervalue, String ordertype) {
        findCustomSearch(searchModel,search,ordervalue,ordertype);
        return Response.success(responsedata);
    }

    @Override
    public void findCustomSearch(SearchModel searchModel, String search, String ordervalue, String ordertype) {
        findDefaultAll(searchModel,ordervalue,ordertype);
    }

    @Override
    public void filterSearch(SearchModel searchModel, String search) {

    }

    @Override
    public void findDefaultAll(SearchModel searchModel, String ordervalue, String ordertype) {
        int currentPage=searchModel.getCalCurrentPage();
        int lineSize = searchModel.getLength();
        String bucketName=searchModel.getName();
        List<NOSObjectSummary> listResult = new ArrayList<NOSObjectSummary>();
        ListObjectsRequest listObjectsRequest = new ListObjectsRequest();
        listObjectsRequest.setBucketName(bucketName);
        listObjectsRequest.setMaxKeys(lineSize);
        ObjectListing listObjects = nosClient.listObjects(listObjectsRequest);
        do {
            listResult.addAll(listObjects.getObjectSummaries());
            if (listObjects.isTruncated()) {
                ListObjectsRequest request = new ListObjectsRequest();
                request.setBucketName(listObjectsRequest.getBucketName());
                request.setMarker(listObjects.getNextMarker());
                listObjects =  nosClient.listObjects(request);
            } else {
                break;
            }
        } while (listObjects != null);
        this.processPageList(listResult);
    }

    @Override
    public void processPageList(List<NOSObjectSummary> list) {
        List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
        for(Iterator<NOSObjectSummary> it = list.iterator(); it.hasNext();){
            NOSObjectSummary bean=it.next();
            Map<String,Object>cellMap=new HashMap<String, Object>();
            cellMap.put("id", bean.getKey());
            cellMap.put("key", bean.getKey());
            cellMap.put("url", NosKeys.ENDPOINT+"/"+bean.getKey());
            cellMap.put("size", bean.getSize());
            cellMap.put("updatetime", DateTools.formateLongDateToString(bean.getLastModified().getTime()));
            data.add(cellMap);
        }
        responsedata.put(TagWords.DATA, data);

    }

    @Override
    public boolean checkDuplicates(String str) {
        return false;
    }
}
